package servlet;

import java.io.IOException;
import org.bson.Document;
import org.bson.types.ObjectId;

import com.mongodb.client.MongoCursor;

import mongo.Class;
import mongo.Major;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(description = "classProcess", urlPatterns = { "/manager/class/classServlet" })
public class classServlet extends HttpServlet{
	private Class Class = new Class();
	private Major Major = new Major();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
		
		String method = request.getParameter("method");
		if(method.equals("add")){
			this.add(request, response);
		}else if(method.equals("prepareAdd")){
			this.prepareAdd(request, response);
		}else if(method.equals("delete")){
			this.delete(request, response);
		}else if(method.equals("update")){
			this.update(request, response);
		}else if(method.equals("prepareUpdate")){
			//this.prepareUpdate(request, response);
		}else if(method.equals("list")){
			this.list(request, response);
		}else{
//			request.getRequestDispatcher("404.jsp").forward(request, response); 添加错误页面
		}
	
	}
	
	
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Boolean result = null ;
		try{
			    String id = request.getParameter("id");
				String name = request.getParameter("name");
				String remark = request.getParameter("remark");
				String major = request.getParameter("major");
				ObjectId majorid = new ObjectId(request.getParameter("majorid"));
				Document newDeparment = new Document("name",name).
						append("remark", remark).
						append("major", major).
						append("majorid", majorid).append("id", id);
				if(Class.isExist(id)){
					//ID存在，不允许添加
					request.setAttribute("error","班级ID已存在！");
					request.getRequestDispatcher("classServlet?method=prepareAdd").forward(request, response);
				}else{
					result = Class.add(newDeparment);
					if(result) {
						this.list(request, response);
					}
				}
			//TODO针对返回结果和所属模块，返回到相对应的页面
		}catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            //TODO发生异常,跳转到对应页面
        }
	}
	
	public void prepareAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        try {
    		MongoCursor<Document> result = Major.list();
    		request.setAttribute("result", result);
    	    request.getRequestDispatcher("addClass.jsp").forward(request, response);
    		//返回了所有专业
    		//TODO针对返回结果和所属模块，返回到相对应的页面  
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        try {
    		ObjectId _id = new ObjectId(request.getParameter("_id"));
    		Boolean result = Class.delete(_id);
    		if(result) {
				this.list(request, response);
			}
    		//TODO针对返回结果和所属模块，返回到相对应的页面  
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }
	}
	
//	public void prepareUpdate(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//        try {
//        	MongoCursor<Document> result = Major.list();
//    		//返回了所有专业
//    		//TODO针对返回结果和所属模块，返回到相对应的页面  
//        }catch(Exception e){
//            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
//          //TODO发生异常,跳转到对应页面
//        }
//	}
	
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Boolean result = null;
		
        try {
    			ObjectId _id = new ObjectId(request.getParameter("_id"));
    			String id = request.getParameter("id");
    			String name = request.getParameter("name");
    			String remark = request.getParameter("remark");
    			String major = request.getParameter("major");
    			ObjectId majorid = new ObjectId(request.getParameter("majorid"));
    			Document newDeparment = new Document("name",name).
    					append("remark", remark).
    					append("major", major).
    					append("majorid", majorid).append("id", id);
    			result = Class.update(_id,newDeparment);
    			if(result) {
    				this.list(request, response);
    			}
    		//TODO针对返回结果和所属模块，返回到相对应的页面
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }
		
	}
	
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
        try {
    		MongoCursor<Document> result = Class.list();
    		request.setAttribute("result", result);
    	    request.getRequestDispatcher("classList.jsp").forward(request, response);
    		//TODO针对所属模块，返回到相对应的页面   
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }

	}
	
}
